﻿// <copyright file="Main.cs" company="The University of Scranton"> 
// Copyright (c) 2010 All Rights Reserved, http://sdpmt.codeplex.com 
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY  
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE. 
// 
// </copyright> 
// <author>Luke Benedict</author> 
// <email>luke.benedict@ieee.org</email> 
// <date>2010-11-04</date> 
// <summary>Contains a static class that facilitates communication with Minitab</summary>

namespace Sdpmt
{
    #region Namespace Usage

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;

    #endregion
    
    /// <summary>
    /// The main form for the SDPMT application
    /// </summary>
    public partial class Main : Form
    {
        #region Private Members

        /// <summary>
        /// The beginning date for the data being analyzed
        /// </summary>
        private DateTime m_BeginDate;

        /// <summary>
        /// The ending date for the data being analyzed
        /// </summary>
        private DateTime m_EndDate;

        /// <summary>
        /// The data to analyze
        /// </summary>
        private System.Collections.Generic.SortedDictionary<DateTime, double> m_SortedDictionary;

        #endregion

        #region Internal Properties

        /// <summary>
        /// The data to analyze
        /// </summary>
        internal System.Collections.Generic.SortedDictionary<DateTime, double> SortedDictionary
        {
            set { m_SortedDictionary = new SortedDictionary<DateTime, double>(value); }
        }        

        /// <summary>
        /// The beginning date for the data being analyzed
        /// </summary>
        internal DateTime BeginDate
        {
            get { return m_BeginDate; }
            set { m_BeginDate = value; }
        }

        /// <summary>
        /// The ending date for the data being analyzed
        /// </summary>
        internal DateTime EndDate
        {
            get { return m_EndDate; }
            set { m_EndDate = value; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// CTOR
        /// </summary>
        public Main()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Handles when the Exit is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handle when file selection happens
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileSelectionUC1_MyEvent(object sender, FileSelectionEventArgs e)
        {
            m_SortedDictionary = new SortedDictionary<DateTime,double>(e.Data);
            fittedLinePlotUC.SortedDictionary = m_SortedDictionary;
            trendAnalysisUC1.SortedDictionary = m_SortedDictionary;
        }

        /// <summary>
        /// Handles when the About is selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox a = new AboutBox();
            a.ShowDialog();
        }

        #endregion

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
        internal static extern IntPtr GetFocus();

        private Control GetFocusedControl()
        {
            Control focusedControl = null;
            // To get hold of the focused control: 
            IntPtr focusedHandle = GetFocus();
            if (focusedHandle != IntPtr.Zero)
                // Note that if the focused Control is not a .Net control, then this will return null. 
                focusedControl = Control.FromHandle(focusedHandle);
            return focusedControl;
        } 


        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox activeTextBox = GetFocusedControl() as TextBox;

            if (activeTextBox != null)
            {
                Clipboard.SetText(activeTextBox.SelectedText);
                activeTextBox.Text = activeTextBox.Text.Remove(activeTextBox.SelectionStart, activeTextBox.SelectionLength);
            }
           
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox activeTextBox = GetFocusedControl() as TextBox;


            if ((activeTextBox != null) && (activeTextBox.SelectionLength > 0))
            {
                this.cutToolStripMenuItem.Enabled = true;
                this.copyToolStripMenuItem.Enabled = true;
            }
            else
            {
                this.cutToolStripMenuItem.Enabled = true;
                this.copyToolStripMenuItem.Enabled = true;
            }

            if (Clipboard.ContainsText() == true)
            {
                this.pasteToolStripMenuItem.Enabled = true;
            }
            else
            {
                this.pasteToolStripMenuItem.Enabled = false;
            }

        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox activeTextBox = GetFocusedControl() as TextBox;

            if (activeTextBox != null)
            {
                Clipboard.SetText(activeTextBox.SelectedText);                
            }
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox activeTextBox = GetFocusedControl() as TextBox;
            int selectionStart;

            if (activeTextBox != null)
            {

                selectionStart = activeTextBox.SelectionStart;

                if (activeTextBox.SelectionLength > 0)
                {
                    activeTextBox.Text = activeTextBox.Text.Remove(selectionStart, activeTextBox.SelectionLength);
                }

                activeTextBox.Text = activeTextBox.Text.Insert(selectionStart, Clipboard.GetText());

                activeTextBox.SelectionStart = activeTextBox.Text.Length;
            }
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox activeTextBox = GetFocusedControl() as TextBox;

            if (activeTextBox != null)
            {
                activeTextBox.SelectAll();
            }

        }
   
    } // end class

} // end namespace
